package com.liang.maogouyuyin.local

import android.content.Context
import android.content.SharedPreferences
import com.liang.maogouyuyin.bean.*
import com.liang.maogouyuyin.local.dao.*

object LocalRepository {
    private const val NAME = "LocalPreferences"
    private const val MODE = Context.MODE_PRIVATE
    private lateinit var sharedPreferences: SharedPreferences
    private lateinit var database: AppDatabase
    private lateinit var petDao: PetDao
    private lateinit var expenditureDao: ExpenditureDao
    private lateinit var weightDao: WeightDao
    private lateinit var anomalyDao: AnomalyDao
    private lateinit var casebookDao: CasebookDao

    fun init(context: Context) {
        sharedPreferences = context.getSharedPreferences(NAME, MODE)
        database = AppDatabase.getInstance(context)
        petDao = database.petDao()
        expenditureDao = database.expenditureDao()
        weightDao = database.weightDao()
        anomalyDao = database.anomalyDao()
        casebookDao = database.casebookDao()
    }

    private val editor: SharedPreferences.Editor
        get() = sharedPreferences.edit()

    var currentPet: String
        get() = sharedPreferences.getString("currentPet", "").toString()
        set(value) = editor.putString("currentPet", value).apply()

    suspend fun savePet(bean: PetBean) {
        if (currentPet.isEmpty())
            currentPet = bean.name
        petDao.save(bean)
    }

    suspend fun deletePet(bean: PetBean) {
        petDao.delete(bean)
    }

    suspend fun updatePet(bean: PetBean) {
        petDao.update(bean)
    }

    suspend fun loadPet(): ArrayList<PetBean> {
        return petDao.loadALl() as ArrayList
    }

    suspend fun loadPetByName(name: String): PetBean {
        return petDao.loadByName(name)
    }

    suspend fun saveExpenditure(bean: ExpenditureBean) {
        expenditureDao.save(bean)
    }

    suspend fun deleteExpenditure(bean: ExpenditureBean) {
        expenditureDao.delete(bean)
    }

    suspend fun updateExpenditure(bean: ExpenditureBean) {
        expenditureDao.update(bean)
    }

    suspend fun loadExpenditure(name: String): ArrayList<ExpenditureBean> {
        return expenditureDao.loadByName(name) as ArrayList
    }

    suspend fun saveWeight(bean: WeightBean) {
        weightDao.save(bean)
    }

    suspend fun deleteWeight(bean: WeightBean) {
        weightDao.delete(bean)
    }

    suspend fun updateWeight(bean: WeightBean) {
        weightDao.update(bean)
    }

    suspend fun loadWeight(name: String): ArrayList<WeightBean> {
        return weightDao.loadByName(name) as ArrayList
    }

    suspend fun saveAnomaly(bean: AnomalyBean) {
        anomalyDao.save(bean)
    }

    suspend fun deleteAnomaly(bean: AnomalyBean) {
        anomalyDao.delete(bean)
    }

    suspend fun updateAnomaly(bean: AnomalyBean) {
        anomalyDao.update(bean)
    }

    suspend fun loadAnomaly(name: String): ArrayList<AnomalyBean> {
        return anomalyDao.loadByName(name) as ArrayList
    }

    suspend fun saveCasebook(bean: CasebookBean) {
        casebookDao.save(bean)
    }

    suspend fun deleteCasebook(bean: CasebookBean) {
        casebookDao.delete(bean)
    }

    suspend fun updateCasebook(bean: CasebookBean) {
        casebookDao.update(bean)
    }

    suspend fun loadCasebook(name: String): ArrayList<CasebookBean> {
        return casebookDao.loadByName(name) as ArrayList
    }
}